﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///GridViewFoot 的摘要说明
///
/// </summary>
public class UtilsControls
{
    private static int pageSize = 10;
    /// <summary>
    /// 设置分页模板里的总记录数
    /// </summary>
    /// <param name="gv">GridView</param>
    /// <param name="TotalRecordCount">总记录数</param>
    public static void SetRecordCountInGridView(GridView gv, int TotalRecordCount)
    {
        GridViewRow gvr = gv.BottomPagerRow;
        if (gvr != null)
        {
            Label l = gvr.FindControl("lblTotalRecord") as Label;//lblTotalRecord在皮肤gvw_Skin_SystemManae里定义的
            if (l != null)
            {
                l.Text = TotalRecordCount.ToString();
            }
        }
    }

    /// <summary>
    /// 设置分页模板里 当前第n/m页功能
    /// </summary>
    /// <param name="gv">GridView</param>
    /// <param name="CurrentPageIndex">当前页</param>
    public static void SetCurrentPageIndex(GridView gv, int CurrentPageIndex)
    {
        GridViewRow gvr = gv.BottomPagerRow;
        if (gvr != null)
        {
            Label l = gvr.FindControl("lblCurrentPageIndex") as Label;//lblCurrentPageIndex在皮肤gvw_Skin_SystemManae里定义的
            if (l != null)
            {
                l.Text = CurrentPageIndex.ToString() + "/" + gv.PageCount.ToString();

                //这里每次都重新给text框里的每页行数 重新赋值
                TextBox tbxPageSize = gvr.FindControl("tbxPageSize") as TextBox;
                tbxPageSize.Text = gv.PageSize.ToString();   //GetPageSizeCookie().ToString();
            }
        }
    }

    ///// <summary>
    ///// 设置分页的大小
    ///// </summary>
    ///// <param name="PageSize">分页的大小</param>
    public static void SetPageSizeCookie(int PageSize)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("GvwPageSize");
        if (cookie == null)
            cookie = new HttpCookie("GvwPageSize");
        cookie.Value = PageSize.ToString();
        TimeSpan ts = new TimeSpan(365, 0, 0, 0);
        cookie.Expires = DateTime.Now + ts;
        HttpContext.Current.Request.Cookies.Set(cookie);

    }

    ///// <summary>
    ///// 获取分页的大小
    ///// </summary>
    ///// <param name="PageSizeCookieName">分页大小的cookie名</param>
    ///// <returns></returns>
    public static int GetPageSizeCookie()
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["GvwPageSize"];
        if (cookie == null || cookie.Value == string.Empty || cookie.Value == null)
            return pageSize;
        else
            return Convert.ToInt32(cookie.Value);
    }
}
